import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:oramemo/common/styles/colors.dart';

import 'preview_logic.dart';
import 'preview_state.dart';

class PreviewPage extends StatelessWidget {
  const PreviewPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final PreviewLogic logic = Get.put(PreviewLogic());
    final PreviewState state = Get.find<PreviewLogic>().state;

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios, color: Colors.black),
          onPressed: () => Get.back(),
        ),
        // title: Text(
        //   '预览',
        //   style: TextStyle(
        //     color: Colors.black,
        //     fontSize: 18.sp,
        //     fontWeight: FontWeight.w500,
        //   ),
        // ),
        centerTitle: true,
      ),
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 16.w),
                child: Obx(() => Column(
                  children: [
                    for (var content in state.generatedContents)
                      _buildContentCard(content, logic),
                  ],
                )),
              ),
            ),
          ),
          _buildBottomButtons(logic),
        ],
      ),
    );
  }

  Widget _buildContentCard(GeneratedContent content, PreviewLogic logic) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(height: 11.h,),
        _buildUserInfo(),
        Container(
          decoration: BoxDecoration(
            color: Color(0xFFF9FBFF),
            borderRadius: BorderRadius.circular(16.r),
            border: Border.all(
              color: Color(0x262574FF),
              width: 1.w,
            ),
          ),
          child: Column(
            children: [
              Padding(
                padding: EdgeInsets.all(16.w),
                child: Text(
                  content.text,
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: Colours.textDeepGray,
                    // height: 1,
                  ),
                ),
              ),
              Padding(
                padding: EdgeInsets.only(right: 16.w, bottom: 16.h),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    GestureDetector(
                      onTap: () => logic.deleteContent(content),
                      child: Text(
                        '删除',
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: Colours.text,
                        ),
                      ),
                    ),
                    SizedBox(width: 20.w),
                    GestureDetector(
                      onTap: () => logic.toggleSelection(content),
                      child: Text(
                        content.isSelected ? '已选择' : '未选择',
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: content.isSelected ? Colours.app_main : Colours.text,
                          fontWeight: content.isSelected ? FontWeight.w500 : FontWeight.normal,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildUserInfo() {
    return Padding(
      padding: EdgeInsets.all(10.w),
      child: Row(
        children: [
          Container(
            width: 40.w,
            height: 40.h,
            decoration: BoxDecoration(
              color: Colors.grey.shade300,
              shape: BoxShape.circle,
            ),
          ),
          SizedBox(width: 12.w),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '用户名',
                style: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w500,
                  color: Colors.black,
                ),
              ),
              SizedBox(height: 0.h),
              Text(
                'xx年xx月xx日',
                style: TextStyle(
                  fontSize: 12.sp,
                  color: Colours.text_gray,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildBottomButtons(PreviewLogic logic) {
    return Container(
      padding: EdgeInsets.fromLTRB( 55.w,10.h,55.w,30.h,),
      child: Row(
        children: [
          Expanded(
            child: GestureDetector(
              onTap: () => logic.regenerateContent(),
              child: Container(
                width: 140.w,
                height: 48.h,
                decoration: BoxDecoration(
                  color: Colors.blue,
                  borderRadius: BorderRadius.circular(12.r),
                ),
                alignment: Alignment.center,
                child: Text(
                  '重新生成',
                  style: TextStyle(
                    fontSize: 16.sp,
                    color: Colors.white,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ),
          ),
          SizedBox(width: 13.w),
          Expanded(
            child: GestureDetector(
              onTap: () => logic.completeSelection(),
              child: Container(
                width: 140.w,
                height: 48.h,
                decoration: BoxDecoration(
                  color: Colors.green,
                  borderRadius: BorderRadius.circular(12.r),
                ),
                alignment: Alignment.center,
                child: Text(
                  '完成',
                  style: TextStyle(
                    fontSize: 16.sp,
                    color: Colors.white,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
